package org.csu.assemblyroom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.catalina.filters.ExpiresFilter;
import org.csu.assemblyroom.common.CommonResponse;
import org.csu.assemblyroom.entity.User;
import org.csu.assemblyroom.persistence.UserMapper;
import org.csu.assemblyroom.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("UserService")
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public CommonResponse<User> addUser(User user){
        int temp= userMapper.insert(user);
        if(temp==0){
            return CommonResponse.createForSuccessMessage("添加用户失败！请点击+http://localhost:8080/user/searchAll");
        }else{
            return CommonResponse.createForError("添加用户失败!");
        }
    }
    @Override
    public  CommonResponse<User> searchUser(String userid){
   try{
       User user=userMapper.selectById(userid);
       if(user==null){
           return CommonResponse.createForError("没有用户,请点击+http://localhost:8080/user/searchAll");
       }
       return CommonResponse.createForSuccess("查询成功",user);

   }catch (Exception e){
       return CommonResponse.createForError("查询失败,请点击+http://localhost:8080/user/searchAll");
   }
    }
    @Override
    public CommonResponse<List<User>> searchAllUser(){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        List<User> userList=userMapper.selectList(queryWrapper);
        System.out.println(userList);
        return CommonResponse.createForSuccess("查询成功",userList);
    }
    @Override
    public CommonResponse<User> deleteUser(String userid){
//    User user=userMapper.deleteById(userid);
    int temp=userMapper.deleteById(userid);
    if(temp>0){
        return CommonResponse.createForSuccessMessage("删除成功,请点击+http://localhost:8080/user/searchAll");
    }else{
        return CommonResponse.createForError("删除失败");
    }
    }
    public CommonResponse<User> updateUser(User user){
   int temp=userMapper.updateById(user);
   if(temp>0){
       return CommonResponse.createForSuccessMessage("更新成功,请点击+http://localhost:8080/user/searchAll");
   }else{
       return CommonResponse.createForError("更新失败,请点击+http://localhost:8080/user/searchAll");
   }
    }
}
